/*******************************************************************
 *
 * Copyright (C), 2021-2022, Letstrong, All rights reserved.
 *
 * Author      : 
 * Create Time : 2020-12-14
 * Version     : v0.1
 * Description : app_global_data header file
 *******************************************************************/
 
#ifndef _APP_GLOBAL_DATA_H__
#define _APP_GLOBAL_DATA_H__

/*----------------------------Head file----------------------------*/
#include "user_config.h"

/*----------------------------macro file---------------------------*/
#define BATT_DISP_PERCENT_UNVALID	0xFF

#define SKIP_RECORD_MAX_NUM         10

/*----------------------------type define--------------------------*/
typedef enum {
	DEV_MODE_IDLE = 0,
	DEV_MODE_SLEEP,
	DEV_MODE_DEEP_SLEEP,
	DEV_MODE_PWR_ON,
	DEV_MODE_SKIP,
	DEV_MODE_NORMAL_MAX,

	DEV_MODE_WARN,
	DEV_MODE_OTA_ING,
	DEV_MODE_OTA_DONE,
	DEV_MODE_OTA_FAIL,
	
	DEV_MODE_PCB_TEST,
	DEV_MODE_DEV_TEST,

	// ...
	DEV_MODE_MAX,
} dev_mode_t;

typedef enum{
	BLE_CONN_TYPE_NULL,
	BLE_CONN_TYPE_PHONE_APP,
	BLE_CONN_TYPE_ROPE_CENTRAL,
	BLE_CONN_TYPE_AIS,

	// ...
	BLE_CONN_TYPE_MAX,
} ble_conn_t;

typedef enum{
	BATT_STATE_NOMAL = 0,
	BATT_STATE_CHARGING,
	BATT_STATE_CHARGE_FULL,
	BATT_STATE_LOWBAT_WARN,
	BATT_STATE_LOW_LOCK,

	// ...
	BATT_STATE_MAX,
} batt_state_t;

typedef enum {
	SKIP_MODE_TIME = 0,
	SKIP_MODE_COUNT = 1,
	SKIP_MODE_RANDOM = 2,
    SKIP_MODE_CUMULATIVE_NUM = 3,
	
	// ...
	SKIP_MODE_MAX,
	SKIP_MODE_NULL = 0xFF,
} skip_mode_t;


#pragma pack(1)

typedef struct {
	uint16_t skip_sec;
	uint16_t skip_cnt;
} skip_group_t;

typedef struct {
	uint8_t data_low    :4;
	uint8_t data_high   :4;
} skip_record_t;


typedef struct {
	uint32_t start_utc;
	uint8_t mode;
	uint16_t setting;
	uint16_t skip_sec_sum;
	uint16_t skip_cnt_sum;

	uint16_t freq_avg;		//Unit: counts/minutes
	uint16_t freq_max;		//Unit: counts/minutes
	uint16_t consecutive_skip_max_num;
	
	uint8_t trip_num;
	skip_group_t group[SKIP_GROUP_MAX_NUM];
	uint16_t skip_valid_sec;//Patch for master calorie algorithm.
} skip_packet_t;			//220 bytes


typedef struct {
	uint8_t 	mode;
	uint8_t		ble_conn_type;
	uint16_t	rst_reason;
	struct {
		uint8_t		exit_ship_mode 	:1;
		uint8_t		ble_conn		:1;
		uint8_t		btn_lock		:1;
		uint8_t		skip_done		:1;
		uint8_t		motor_running	:1;
		uint8_t		tx_to_central	:1;
		uint8_t		pcb_test		:1;
#if(DEV_MAC_BURN_ENABLE)
		uint8_t		dev_mac_valid	:1;
#else
		uint8_t		reserve			:1;
#endif
	} state;
} app_system_data_t;


typedef struct {
	uint8_t			tmp_skip_mode;
	uint16_t		cnt_down_start;
#if(SCHOOL_ADV_ENABLE)
#if(LEIJISHU == 0)
    uint8_t         trip_group[3];      //Time unit: 20s
    uint8_t         skip_group[3];      //Time unit: 20s
#else
    uint16_t		tmp_skip_record;
    skip_record_t   skip_record[SKIP_RECORD_MAX_NUM];
#endif
#endif
	skip_packet_t	skip;
    uint32_t        utc;
} app_tmp_data_t;


typedef struct {
	uint16_t timezone_mins;
	uint16_t cnt_down_secs;
	uint16_t cnt_down_jumps;
} app_user_info_t;

typedef struct {
	uint8_t state;
	uint16_t disp_percent;
}app_batt_info_t;

typedef struct {
	uint8_t		tim_cnt;
} app_disp_data_t;


typedef struct {
	app_system_data_t	sys;
	app_tmp_data_t		tmp;
	app_user_info_t		user;
	app_batt_info_t		batt;
	app_disp_data_t		disp;
} app_global_data_t;


typedef struct {
    uint8_t mac_valid   :1;
    uint8_t skip_state  :2;
    uint8_t reserve     :5;
}app_dev_adv_state_t;

#pragma pack()

/*--------------------------var declarative------------------------*/
extern app_global_data_t g_app_data;

/*-------------------------func declaration------------------------*/
extern void app_global_data_init(void);

/*-----------------------------------------------------------------*/

#endif // _APP_GLOBAL_DATA_H__
